﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLDanToc
    {
        List<DanToc> list;
        QuanLySVDataContext db;
        

        // constructor
        public BLLDanToc()
        {
            this.list = new QuanLySVDataContext().DanTocs.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 dan toc co ton tai hay khong
        public bool checkExist(DanToc temp)
        {
            foreach (DanToc dt in list)
            {
                if (dt.DanTocID.Equals(temp.DanTocID))
                    return true;
            }
            return false;
        }

        // update thong tin dan toc
        public bool updateDanToc(DanToc update)
        {
            try
            {                

                var temp = (from c in db.DanTocs
                            where c.DanTocID == update.DanTocID
                            select c).First();
                DanToc found = (DanToc)temp;
                found.DanTocID = update.DanTocID;
                found.TenDanToc = update.TenDanToc;
                found.GhiChu = update.GhiChu;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them dan toc moi vao database
        public bool insertDanToc(DanToc temp)
        {
            try
            {
                db.DanTocs.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa dan toc
        public bool deleteDanToc(DanToc temp)
        {
            try
            {
                var dt = (from c in db.DanTocs
                          where c.DanTocID == temp.DanTocID
                          select c).First();
                DanToc found = (DanToc)dt;

                db.DanTocs.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac dan toc
        public List<DanToc> returnListDanToc()
        {
            this.list = new QuanLySVDataContext().DanTocs.ToList();
            return list;
        }

        // tra ve 1 dan toc theo so thu tu trong list
        public DanToc retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().DanTocs.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 dan toc theo ma dan toc
        public DanToc retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().DanTocs.ToList();
            foreach (DanToc temp in list)
                if (temp.DanTocID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 dan toc co hop le hay khong
        public bool kiemTra(DanToc temp)
        {
            if (temp.DanTocID == "") return false;
            return true;
        }
    }
}
